<?php
namespace Home\Controller;
use Think\Controller;
class CoachController extends CommonController {

    //显示教练列表
    public function coachlist(){
        $coach=M('coach');
        //$this->coachlist=$coach->select();
        $this->coachlist=$coach->join('club on coach.coach_club_id=club.club_id')->select();
        //var_dump($this);

        //显示编号
        $cnt=1;
        $this->assign('cnt',$cnt);

        $this->display();
    }



    //删除教练
    public function delcoach($coachId){

        $Coach = M('coach');

        $photoName = ($Coach->select($coachId))['0']['coach_photo'];
        if ($photoName != 'default.png') { //如果不是默认文件
            unlink('./Public/upload/coachpics/' . $photoName); //删除对应文件
        }
        if ($Coach->delete($coachId)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //显示添加比赛信息界面
    public function coachAdd()
    {
        $this->display();
    }

    //添加教练
    public function docoachadd()
    {
        $coach = D("coach");

        if (!$coach->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            exit($coach->getError());
        } else {
            // 创建成功，做其他事情
            if ($coach->create()) {
                $pname = 'default.jpg';
                if ($picinfo = $this->upload()) {
                    $pname = $picinfo['file']['savename'];
                }
            }
            $coach->coach_photo = $pname;

            //默认密码为123456
            $coach->coach_pwd = md5('123456');

            if ($coach->add()) {
                $this->success('添加成功' ,'./coachList', 3);
            } else {
                $this->error("添加失败");
            }
        }

     }

    //文件上传upload函数
    public function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/upload/coachpics/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $upload->saveName = time() . mt_rand(); //实际保存文件名：时间戳+随机数
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
//            $this->success('上传成功！');
            return $info;
        }
    }


    //显示教练详情
    public function coachmod($coach_id)
    {

        $coach = M('coach');

        $this->coachInfo = $coach->select($coach_id);
        $this->display();
    }

    //修改教练详情
    public function docoachmod($coach_id)
    {

        $Coach = D('coach');

        $coachInfo = $Coach->select($coach_id);

        if (!$Coach->create()) {
//             如果创建失败 表示验证没有通过 输出错误提示信息
            exit($Coach->getError());
        } else {
            // 创建成功，做其他事情
            if ($Coach->create()) {
                //判断是否上传了新的图片
                if ($_FILES['file']['error'] == 0) { //如果有文件上传
                    //删除之前的文件
                    unlink('./Public/upload/coachpics/' . $coachInfo['0']['coach_photo']);
                    //上传新的文件
                    $pname = 'default.jpg';
                    if ($picinfo = $this->upload()) {
                        $pname = $picinfo['file']['savename'];
                    }
                    $Coach->coach_photo = $pname;
                }
                $Coach->coach_id = $coach_id;
                if ($Coach->save() !== false) {
                    $this->success('修改成功', './coachList', 3);
                } else {
                    $this->error("修改失败");
                }
            }
        }
    }




//重置密码
    public function coachpwd($coach_id)
    {
        //echo $coach_id;
<<<<<<< HEAD
        $data['coach_pwd']='123456';
=======
        $data['coach_pwd']=md5('123456');
>>>>>>> 62e237362e20a94a945bc25068926c792b5c027b
        $coach=M('coach');
        $res=$coach->where("coach_id=$coach_id")->save($data);
        // echo $coach->getLastSql();//显示执行的sql语句
        // echo $res;
        if(false!== $res){
            $this->success('重置密码成功！');
        }else{
            $this->error('重置密码失败！');
        }

    }


<<<<<<< HEAD
    public function interfaceCoach()
    {
        //$id = I('get.id');

        $coach = M('coach');
        $result = $coach->select();
        if($result!=false)
        {
            $data = array('results' => $result);
            $this->ajaxReturn($data,'JSON');
        }
        else{
            echo "获取失败!";
        }
    }

    public function interfaceCoachInfo()
    {
        $id=I('get.coach_id');
        $coachInfo=M('coach');
        $result = $coachInfo->where("coach_id=$id")->find();
        if($result!=false)
        {
            $data = array('results' => $result);
            $this->ajaxReturn($data,'JSON');
        }
        else{
            echo "获取失败!";
        }
    }



=======
>>>>>>> 62e237362e20a94a945bc25068926c792b5c027b



}